package com.yunhe.util;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

//通过set方法设置属性的形式，创建c3p0连接池对象

public class C3P0Test {
    static ClassPathXmlApplicationContext cpac = null;

    static {
        cpac = new ClassPathXmlApplicationContext("applicationContext.xml");
    }

    @Test
    public void c3p0() throws Exception {
        //创建数据源
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        //设置数据库连接参数
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mybatis");
        dataSource.setUser("root");
        dataSource.setPassword("admin");
        //获得连接对象
        Connection connection = dataSource.getConnection();
        System.out.println(connection);


    }

    @Test
    public void spring_c3p0() throws Exception {
        DataSource ds= (DataSource) cpac.getBean("c3p0");
        Connection connection = ds.getConnection();
        System.out.println(connection);
    }
    @Test
    public void datasource() throws Exception {
        DataSource ds= (DataSource) cpac.getBean("dataSource");
        Connection connection = ds.getConnection();
        System.out.println(connection);
    }


}
